package com.czl.module_storehouse.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.SizeUtils;
import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.fragment.BaseFragment1;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.databinding.FragmentStockListBinding;
import com.czl.module_storehouse.mvp.presenter.StockPresenter;
import com.czl.module_storehouse.widget.ColorFlipPagerTitleView;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
public class StockListFragment extends BaseFragment1<FragmentStockListBinding> implements SimpleView {

    private final List<String> mDataList = new ArrayList<>();
    private final List<Fragment> fragments = new ArrayList<>();

    @InjectPresenter
    StockPresenter mStockPresenter;

    public static Fragment getInstance(String tag) {
        StockListFragment fragment = new StockListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tag", tag);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected FragmentStockListBinding getBinding(LayoutInflater layoutInflater, ViewGroup container) {
        return FragmentStockListBinding.inflate(layoutInflater, container, false);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }

    private void initMagicIndicator() {
        binding.indicator.setBackgroundColor(Color.parseColor("#fafafa"));
        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(Color.parseColor("#808080"));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(requireContext(), R.color.color_25A468));
                simplePagerTitleView.setOnClickListener(v -> binding.viewPager2.setCurrentItem(index));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(SizeUtils.dp2px(6));
                indicator.setLineWidth(SizeUtils.dp2px(10));
                indicator.setRoundRadius(SizeUtils.dp2px(3));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(ContextCompat.getColor(requireContext(), R.color.color_25A468));
                return indicator;
            }
        });
        binding.indicator.setNavigator(commonNavigator);
        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.indicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                binding.indicator.onPageScrollStateChanged(state);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                binding.indicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
        });
        FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager(), getLifecycle());
        binding.viewPager2.setAdapter(adapter);
    }

    @Override
    protected void reload() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            String tag = arguments.getString("tag");
            if (mStockPresenter != null) {
                mStockPresenter.getStorehouseLocatStockInfo(tag);
            }
        }
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        showLoadSuccess();
        ListBean<LocatListBean> listBean = (ListBean<LocatListBean>) t;
        if (listBean == null) {
            return;
        }
        List<LocatListBean> list = listBean.getList();
        if (list == null || list.isEmpty()) {
            return;
        }
        mDataList.clear();
        fragments.clear();
        for (LocatListBean locatListBean : list) {
            mDataList.add(locatListBean.getStorehouseLocatName());
            String locatId = String.valueOf(locatListBean.getStorehouseLocatId());

            fragments.add(StockListChildFragment.getInstance(locatId, locatListBean.getStorehouseLocatName(),
                    new ArrayList<>(locatListBean.getSortList()), locatListBean.getChildList() == null ? new ArrayList<>()
                            : new ArrayList<>(locatListBean.getChildList())));
        }
        initMagicIndicator();
    }

    private class FragmentAdapter extends FragmentStateAdapter {

        public FragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragments.get(position);
        }

        @Override
        public int getItemCount() {
            return fragments.size();
        }
    }
}
